package com.itheima.controller;

import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;


//把它放到spring容器里
@Controller
@RequestMapping
public class UserController {
    @RequestMapping("/quike13")
    @ResponseBody
    public void save13() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.parse("abcde");
    }

    public String login(String username, String password, HttpSession sessin){
        UserService userServic=new UserServiceImpl();
        User user= userServic.login(username,password);
        if (user!=null){
            sessin.setAttribute("user",user);
            return "redirect:/index.jsp";
        }
        return "redirect:login.jsp";
    }

    @RequestMapping("/quike12")
    @ResponseBody
    public void save12(String username , MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        //获取上传文件得名称
        String filename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\upload\\"+filename));
    }

    @RequestMapping("/quike11")
    @ResponseBody
    public void save11(@CookieValue(value = "JSESSIONID") String jsessionid){
        System.out.println(jsessionid);
    }

    @RequestMapping("/quike10")
    @ResponseBody
    public void save10(@RequestHeader(value = "User-Agent",required = false)String user_agent){
        System.out.println(user_agent);
    }

    @RequestMapping("/quike9/{name}")
    @ResponseBody
    public void save9(@PathVariable(value = "name") String username){
        System.out.println(username);
    }


    @RequestMapping("/quike8")
    @ResponseBody
    public void save8(@RequestParam(value = "name",required = false) String username){
        System.out.println(username);
    }

    @RequestMapping("/quike7")
    @ResponseBody
    public void save7 (@RequestBody List<User> userList){
        System.out.println(userList);
    }

    @RequestMapping("/quike6")
    @ResponseBody
    public void save6(String[] strs){
        System.out.println(Arrays.asList(strs));
    }

    @RequestMapping("/quike5")
    @ResponseBody
    public void save5(User user){
        System.out.println(user);
    }


    @RequestMapping("/quike4")
    @ResponseBody
    public User save4(){
        User user=new User();
        user.setAge(18);
        user.setUsername("zhangsan");
        return user;
    }

    @RequestMapping("/quike3")
    @ResponseBody
    public String save3(){
        return "hello world";
    }


    @RequestMapping("/quike2")
    public ModelAndView save2(ModelAndView modelAndView){
        //设置视图名称
        modelAndView.setViewName("success");
        //设置模型数据
        modelAndView.addObject("username","itcast");
        return modelAndView;
    }


    @RequestMapping("/quike")
    public String save(){
        System.out.println("前端控制器执行了..");
        return "success";
    }


}